<?php

/**
 * index
 * @author 刘军 
 * @version 1.0
 * @package application
 */
class Bbs extends Membr_Controller
{
	public function __construct() {
		parent::__construct();
		$this->load->model('member_bbs_publish_model');
		$this->load->model('member_bbs_reply_model');
		$this->load->model('member_bbs_type_model');
	}
	
	//论坛列表
	public function index(){
		
		$data = array();
		$search = array('orders' => array('id'=>'desc'));
		// 总数与分页
		$total = $this->member_bbs_publish_model->total( null, $search );
		$data['total'] = $total;
		$per_page = 10;
		$this->load->library( 'pagination' );
		$pagination_config = array(
				'base_url' => base_url($this->_site_path . '/bbs/index/' ),
				'total_rows' => $total,
				'per_page' => $per_page,
				'uri_segment' => 4,
		);
		//分页
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();
		$search['limit'] = array( 'persize' => $per_page, 'offset' => $this->pagination->get_cur_offset() );
		$data['infos'] = $this->member_bbs_publish_model->all($search);
		$data['css'] = 'bootstrap.css';
		$data['operate'] = 'bbs';
		$this->load->view($this->_site_path . '/bbs/index', $data);
	}
	
	//发表帖子
	public function publish(){
		
		$bbs = $this->input->post();
		if($bbs['dopost']=='add'){
			unset($bbs['dopost']);
			$bbs['user_name'] = $this->session->userdata('user_name');
			$bbs['created'] = $bbs['modified'] = time();
			$bbs_id = $this->member_bbs_publish_model->insert( $bbs );
			if(!$bbs_id){
				$this->error_js('论坛发布失败！');
			}else{
				header('location:index');
			}
		}
		$data = array();
		$data['type_names'] = $this->member_bbs_type_model->get_values('id', 'type_name');
		$data['css'] = 'bootstrap.css';
		$data['operate'] = 'bbs';
		$this->load->view($this->_site_path . '/bbs/publish', $data);
	}

	//回复帖子
	public function reply($id=0){
		
		if(intval($id) <= 0){
			$this->message_js('信息有误！', base_url($this->_site_path . '/bbs/index'));
		}
		$bbs = $this->input->post();
		$data = array();
		$data['publishInfo'] = $this->member_bbs_publish_model->get_by_pk($id);
		if($bbs['parent_id']){
			$bbs['reply_user'] = $this->session->userdata('user_name');
			$bbs['created'] = $bbs['modified'] = time();
			$bbs_id = $this->member_bbs_reply_model->insert( $bbs );
			if(!$bbs_id){
				$this->error_js('回复失败！');
			}
			//增加帖子回复次数
			$attributes = array('replies' =>$data['publishInfo']['replies']+1);
			$this->member_bbs_publish_model->update_by_pk($attributes, $id);
		}else{
			//增加帖子浏览次数
			$attributes = array('clicks' =>$data['publishInfo']['clicks']+1);
			$this->member_bbs_publish_model->update_by_pk($attributes, $id);
		}
		// 总数与分页
		$search = array('attributes'=>array('parent_id'=>$data['publishInfo']['id']), 'orders' => array('id'=>'desc'));
		//p($search);exit;
		$total = $this->member_bbs_reply_model->total( null, $search );
		$data['total'] = $total;
		$per_page = 5;
		$this->load->library( 'pagination' );
		$pagination_config = array(
				'base_url' => base_url($this->_site_path . '/bbs/reply/' . $id  ),
				'total_rows' => $total,
				'per_page' => $per_page,
				'uri_segment' => 5,
		);
		//分页
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();
		$search['limit'] = array( 'persize' => $per_page, 'offset' => $this->pagination->get_cur_offset() );
		$data['replyInfos'] = $this->member_bbs_reply_model->all($search);
		$data['publishInfo']['type_name'] = $this->member_bbs_type_model->get_value_by_pk($data['publishInfo']['type_id'], 'type_name');
		
		$data['css'] = 'bootstrap.css';
		$data['operate'] = 'bbs';
		$this->load->view($this->_site_path . '/bbs/reply', $data);
	}
	
}
